package com.hebeu.group.service.impl;

import com.hebeu.group.mapper.CartMapper;
import com.hebeu.group.pojo.Cart;
import com.hebeu.group.pojo.CartExample;
import com.hebeu.group.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    private CartMapper cartMapper;
    @Autowired
    public CartServiceImpl(CartMapper cartMapper){
        this.cartMapper=cartMapper;
    }

    /**
     * 添加菜品到购物车
     * @param cart
     */
    @Override
    public void insert(Cart cart) {
        cartMapper.insert(cart);
    }


    /**
     * 修改购物车信息
     * @param cart
     */
    @Override
    public void updateCart(Cart cart) {
       cartMapper.updateByPrimaryKey(cart);
    }


    /**
     * 根据user_id获取购物车
     * @param user_id
     * @return
     */
    @Override
    public List<Cart> selectAllCarts(int user_id){
        CartExample example=new CartExample();
        CartExample.Criteria criteria=example.createCriteria();
        criteria.andUserIdEqualTo(user_id);
        List<Cart> cartList=cartMapper.selectByExample(example);
        return cartList;
    }


    /**
     * 获取单个商品条目的购物车
     * @param cart_id
     * @return
     */
    @Override
    public Cart getSingleCart(String cart_id) {
        Cart cart=cartMapper.selectByPrimaryKey(cart_id);
        return cart;
    }


    /**
     * 删除商品
     * @param cartId
     */
    @Override
    public void deleteCart(String cartId) {
        cartMapper.deleteByPrimaryKey(cartId);
    }

    /**
     * 修改有效性
     * @param cart
     */
    @Override
    public void updateEffective(Cart cart) {
        cartMapper.updateByPrimaryKey(cart);
    }
}
